* set path

	global raw "~/agriculture_temperature"
	
	global temp "~/revision_codes/temp"
	global visual "~/revision_codes/visual"
	
	use $raw/data/merged_for_reg, clear

	xtset ctycode year
		
	* set reference bin
	
	drop dd5bin10
********************************************************************************			
************distributed lag model(Figure 7)*************************************
********************************************************************************		
	* SR + IR effects based on distributed lags
	
		foreach x in pre ssd rhu prs win {
			forvalues t = 1/5 {
				qui gen L`t'`x'2 = L`t'`x'^2
				}
			}
		
		
		* northern
		
		estimates clear

		foreach x in farming_output livestock_output forest_output fishery_output{
			foreach t in 5 {
				qui reghdfe ln_`x' L(0/5).(dd5bin0 dd5bin5 dd5binNeg dd5bin15 dd5bin20 dd5bin25 dd5bin30 dd5bin35) ///
				c.pre##c.pre c.ssd##c.ssd c.rhu##c.rhu c.prs##c.prs c.win##c.win ///
				L?pre* L?ssd* L?rhu* L?prs* L?win* ///
					i.pro#c.year##c.year ///
					if inrange(year,1995,2015) & region3==1, ///
					absorb(ctycode year) cluster(ctycode proXyear) 
				estimates store n_`x'_dl`t'
				
				qui reghdfe ln_`x' L(0/5).(dd5bin0 dd5bin5 dd5binNeg dd5bin15 dd5bin20 dd5bin25 dd5bin30 dd5bin35) ///
				c.pre##c.pre c.ssd##c.ssd c.rhu##c.rhu c.prs##c.prs c.win##c.win ///
				L?pre* L?ssd* L?rhu* L?prs* L?win* ///
					i.pro#c.year##c.year ///
					if inrange(year,1995,2015) & region3==2, ///
					absorb(ctycode year) cluster(ctycode proXyear) 
				estimates store s_`x'_dl`t'				
				}
			}			
			
		*esttab n_*_dl*, b(4) se(4) keep(dd5bin*) brackets	r2 star(* 0.1 ** 0.05 *** 0.01)
		*esttab n_*_dl*, b(4) se(4) keep(*dd5bin30 *dd5bin35) brackets	r2 star(* 0.1 ** 0.05 *** 0.01)
		
		foreach x in farming_output livestock_output forest_output fishery_output{
			foreach t in 0 5 15 20 25 30 35 Neg {
				estimates restore n_`x'_dl5
				lincomest L.dd5bin`t'+L2.dd5bin`t'+L3.dd5bin`t'+L4.dd5bin`t'+L5.dd5bin`t'
                parmest,label saving($temp/n_`x'_`t'.dta,replace) idstr("`x'")			
				estimates restore s_`x'_dl5
				lincomest L.dd5bin`t'+L2.dd5bin`t'+L3.dd5bin`t'+L4.dd5bin`t'+L5.dd5bin`t'
                parmest,label saving($temp/s_`x'_`t'.dta,replace) idstr("`x'")			
				}
			}	
		
		
		clear	
		foreach x in farming_output livestock_output forest_output fishery_output{
			foreach t in 0 5 15 20 25 30 35 Neg {
				append using $temp/n_`x'_`t'.dta	
				}
				}
			gen region = "North"
			
		foreach x in farming_output livestock_output forest_output fishery_output{
			foreach t in 0 5 15 20 25 30 35 Neg {
				append using $temp/s_`x'_`t'.dta	
				}
				}			
			replace region = "South" if region == ""
			
		gen level = 2.5 if strmatch(label, "*dd5bin0*")
		replace level = 7.5 if strmatch(label, "*dd5bin5*")
		replace level = -2.5 if strmatch(label, "*dd5binNeg*")
		*replace level = 12.5 if strmatch(label, "*dd5bin10*")
		replace level = 17.5 if strmatch(label, "*dd5bin15*")
		replace level = 22.5 if strmatch(label, "*dd5bin20*")
		replace level = 27.5 if strmatch(label, "*dd5bin25*")
		replace level = 32.5 if strmatch(label, "*dd5bin30*")
		replace level = 37.5 if strmatch(label, "*dd5bin35*")			

		
		// sector name 
		rename idstr sector
		replace sector = "Aggregate" if strmatch(sector,"aoutput")
		replace sector = "Cropping" if strmatch(sector,"farming_output")
		replace sector = "Livestock" if strmatch(sector,"livestock_output")
		replace sector = "Forestry" if strmatch(sector,"forest_output")
		replace sector = "Fishery" if strmatch(sector,"fishery_output")
			
		rename estimate coef 
		rename stderr se 
		rename min95 ci_l
		rename max95 ci_h		
		
	// add zero-value lines
	bysort region sector : gen last = _n == _N
	expand 2 if last
	sort region sector label
	replace level = 12.5 if label==label[_n-1]
	replace coef = 0 if label==label[_n-1]
	replace se = 0 if label==label[_n-1]
	replace ci_h = 0 if label==label[_n-1]
	replace ci_l = 0 if label==label[_n-1]				
		
		save $visual/ir_dl, replace
			
	// delete dta data 
	cd $temp/
	fs *.dta
	foreach f in `r(files)'{
	   erase "`f'"
	}		
			
